package com.ld.hpfs.view.excel;

import java.util.Collection;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFDataFormat;
import org.apache.poi.hssf.usermodel.HSSFFont;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.springframework.web.servlet.view.document.AbstractExcelView;

import com.ld.hpfs.web.interseptor.WebConstants;
public class DealSearchExcelView extends AbstractExcelView{
	@Override
	protected void buildExcelDocument(Map<String, Object> model,
			HSSFWorkbook workbook, HttpServletRequest request,
			HttpServletResponse response) throws Exception {


        // 取得模型数据
  /*  	List<HashMap<String,Object>> table = (List<HashMap<String,Object>>) model.get("table");*/
		HashMap<String,Object> temp = (HashMap<String,Object>) model.get("table");
		List<HashMap<String,Object>> table = (List<HashMap<String,Object>>) temp.get("list");
		String language = (String) temp.get("language");
        String refTitles[] = (String[])temp.get("titles");
//
//        // 创建工作表和标题
        HSSFSheet sheet = workbook.createSheet(); // 创建工作区
//        String titles[] =new String[100];
//        int temp=0;
//        for (HashMap<String,Object> entry : table) {
//        	entry.remove("ID_HEAD_ID");
//        	if(temp==0){
//        		Set set = entry.keySet();
//                Iterator it = set.iterator();
//                int index=0;
//                while(it.hasNext()){             	  
//             	  titles[index++]=it.next().toString();
//                }
//        	}
//        	 temp++;
//        }
        String titles[]= new String[38];
        if(language.equals("en")){
        	String titles_en[]  ={"document_no","document_date","customer_code","customer/supplier","document_type","invoice_code",
        			"invoice_no","invoice_date","tax_no","tax_amount" ,"customer_bankAccount","address/telphone","amount","status",
        			"remark","express_company","express_no","scan_time"
            		,refTitles[0]
            		,refTitles[1]
            		,refTitles[2]
            		,refTitles[3]
                    ,refTitles[4]
                    ,refTitles[5]
                    ,refTitles[6]
                    ,refTitles[7]
                    ,refTitles[8]
                    ,refTitles[9]
                    ,refTitles[10]
                    ,refTitles[11]
                    ,refTitles[12]
                    ,refTitles[13]
                    ,refTitles[14]
                    ,refTitles[15] 
                    ,refTitles[16]
                    ,refTitles[17]
                    ,refTitles[18]
                    ,refTitles[19]
            		};
        	for(int i = 0; i < titles_en.length; i++){
        		titles[i] = titles_en[i];
        	}
        }
        else if(language.equals("zh_CN")){
	        String titles_cn[]={"单据号","单据日期","客户代码","客户/供应商","票据类型","发票代码","发票号码","发票日期","税号","税额",
	             		"客户银行帐号","地址电话","金额","状态","备注","快递公司","快递单号","扫描时间"
	             		,refTitles[0]
	             		,refTitles[1]
	             		,refTitles[2]
	             		,refTitles[3]
	                     ,refTitles[4]
	                     ,refTitles[5]
	                     ,refTitles[6]
	                     ,refTitles[7]
	                     ,refTitles[8]
	                     ,refTitles[9]
	                     ,refTitles[10]
	                     ,refTitles[11]
	                     ,refTitles[12]
	                     ,refTitles[13]
	                     ,refTitles[14]
	                     ,refTitles[15] 
	                     ,refTitles[16]
	                     ,refTitles[17]
	                     ,refTitles[18]
	                     ,refTitles[19]
	             		};  
		        for(int i = 0; i < titles_cn.length; i++){
		    		titles[i] = titles_cn[i];
		    	}
        }
        
        HSSFRow row = sheet.createRow(0);
        HSSFCellStyle items_style = workbook.createCellStyle();
        items_style.setAlignment((short) HSSFCellStyle.ALIGN_CENTER);

        HSSFFont celltbnamefont = workbook.createFont();
        celltbnamefont.setFontHeightInPoints((short) 10);

        celltbnamefont.setColor((short) (HSSFFont.COLOR_RED));
        items_style.setFont(celltbnamefont);
        items_style.setWrapText(true);
		
        for (int i = 0; i < titles.length; i++) {
            HSSFCell cell = row.createCell(i);
            sheet.setColumnWidth(i, 5335);
            cell.setCellValue(titles[i]);
            cell.setCellStyle(items_style);
        }

        HSSFCellStyle datestyle = workbook.createCellStyle();
        HSSFDataFormat df = workbook.createDataFormat();
        datestyle.setDataFormat(df.getFormat("yyyy-mm-dd"));

        int rowNum = 1;
        for (HashMap<String,Object> entry : table) {
            // create the row data     
//           entry.remove("ID_HEAD_ID");
           HSSFRow datarow = sheet.createRow(rowNum++);          
//           Collection collection= entry.values();
//           Iterator its = collection.iterator();
//        	for(int i=0;i<entry.size();i++){           	
//            	datarow.createCell(i).setCellValue(its.next().toString());
//            }
            datarow.createCell(0).setCellValue(entry.get("document_no").toString());
            datarow.createCell(1).setCellValue(entry.get("document_date").toString());
            datarow.createCell(2).setCellValue(entry.get("cust_code").toString());
            datarow.createCell(3).setCellValue(entry.get("cust_name_chn").toString());
            datarow.createCell(4).setCellValue(entry.get("doc_type_name").toString());
            datarow.createCell(5).setCellValue(entry.get("invoice_code").toString());
            datarow.createCell(6).setCellValue(entry.get("invoice_no").toString());
            datarow.createCell(7).setCellValue(entry.get("invoice_date").toString());
            datarow.createCell(8).setCellValue(entry.get("tax_code").toString());
            datarow.createCell(9).setCellValue(entry.get("tax_amount").toString());
            datarow.createCell(10).setCellValue(entry.get("bank_account").toString());
            datarow.createCell(11).setCellValue(entry.get("address").toString());
            datarow.createCell(12).setCellValue(entry.get("total_amount").toString());
            datarow.createCell(13).setCellValue(entry.get("doc_status").toString());
            datarow.createCell(14).setCellValue(entry.get("remark").toString());
            datarow.createCell(15).setCellValue(entry.get("express").toString());
            datarow.createCell(16).setCellValue(entry.get("express_no").toString());
            datarow.createCell(17).setCellValue(entry.get("express_date").toString());
            datarow.createCell(18).setCellValue(entry.get("ref01").toString());
            datarow.createCell(19).setCellValue(entry.get("ref02").toString());
            datarow.createCell(20).setCellValue(entry.get("ref03").toString());
            datarow.createCell(21).setCellValue(entry.get("ref04").toString());
            datarow.createCell(22).setCellValue(entry.get("ref05").toString());
            datarow.createCell(23).setCellValue(entry.get("ref06").toString());
            datarow.createCell(24).setCellValue(entry.get("ref07").toString());
            datarow.createCell(25).setCellValue(entry.get("ref08").toString());
            datarow.createCell(26).setCellValue(entry.get("ref09").toString());
            datarow.createCell(27).setCellValue(entry.get("ref10").toString());
            datarow.createCell(28).setCellValue(entry.get("ref11").toString());
            datarow.createCell(29).setCellValue(entry.get("ref12").toString());
            datarow.createCell(30).setCellValue(entry.get("ref13").toString());
            datarow.createCell(31).setCellValue(entry.get("ref14").toString());
            datarow.createCell(32).setCellValue(entry.get("ref15").toString());
            datarow.createCell(33).setCellValue(entry.get("ref16").toString());
            datarow.createCell(34).setCellValue(entry.get("ref17").toString());
            datarow.createCell(35).setCellValue(entry.get("ref18").toString());
            datarow.createCell(36).setCellValue(entry.get("ref19").toString());
            datarow.createCell(37).setCellValue(entry.get("ref20").toString());
        } 

	}
}
